package com.scalar.chiptrack.jasper.operations.actions;

import com.scalar.chiptrack.jasper.base.AbstractJasperReportsAction;
import com.scalar.chiptrack.jasper.base.JasperReportsHelper;
import com.scalar.chiptrack.jasper.base.datasource.ChiptrackJasperDataSource;
import com.scalar.chiptrack.jasper.base.datasource.JasperDataSourceFactory;
import com.scalar.chiptrack.jasper.JasperReports;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.db.DBUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.ResultSet;

/**
 * User: sujan
 * Date: May 17, 2011
 * Time: 9:23:57 PM
 */
public class ShipmentReportAction extends AbstractJasperReportsAction {
	public ChiptrackJasperDataSource getJasperDataSource(Map paramMap, HttpServletRequest request) throws Exception {
		String reportName = JasperReportsHelper.getReportName(request);
		Connection conn = null;
		CallableStatement cstmt = null;
		ResultSet rset = null;
		ChiptrackJasperDataSource chiptrackJasperDataSource = null;
		if(JasperReports.LOTS_INTRANSIT_REPORT.equalsIgnoreCase(reportName)) {
			try {
				int paramNo = 1;
				conn = DataSource.getConnection();

				cstmt = conn.prepareCall("{Call sp_transit_report_proc(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}");
				cstmt.setString(paramNo++, (String) paramMap.get("prompt0"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt1"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt2"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt3"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt4"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt5"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt6"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt7"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt8"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt9"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt10"));
				cstmt.setString(paramNo++, (String) paramMap.get("prompt11"));

				rset = cstmt.executeQuery();
				chiptrackJasperDataSource = JasperDataSourceFactory.getDataSource(rset);
			} finally {
				DBUtil.close(conn, rset, cstmt);
			}
		}
		return chiptrackJasperDataSource;
	}
}
